package com.comment.util.ip;

import cn.hutool.core.io.resource.ClassPathResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;

import java.io.InputStream;

@Slf4j
public class SearcherIp {
    private static Searcher searcher = null;

    static {
        try {
            InputStream inputStream = new ClassPathResource("ip2region/ip2region.xdb").getStream();
            byte[] dbBinStr = FileCopyUtils.copyToByteArray(inputStream);
            searcher = Searcher.newWithBuffer(dbBinStr);
        } catch (Exception e) {
            log.error("ip2region 初始化失败", e);
        }
    }

    public static String searcherId(String ip) throws Exception {
        String search = searcher.search(ip);
        searcher.close();
        return search;
    }
}
